//------------------------------------------------------------------------------
//  <copyright file="PathPlanningAndTrackingView.xaml.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Robotics.Navigation.Visualization
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Microsoft.Robotics.Navigation.Runtime.Motion;
    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Interaction logic for PathPlannerView.xaml
    /// </summary>
    public partial class PathPlanningAndTrackingView : UserControl
    {
        /// <summary>
        /// Last created instance of this view.
        /// </summary>
        /// <remarks>
        /// This is used to relay information into the agents.
        /// In this case, the goal X/Y coordinates the user has chosen.
        /// </remarks>
        private static PathPlanningAndTrackingView instance = null;

        /// <summary>
        /// Initializes a new instance of the PathPlanningAndTrackingView class
        /// </summary>
        public PathPlanningAndTrackingView()
        {
            this.InitializeComponent();
            Instance = this;
        }

        /// <summary>
        /// Gets or sets last created instance of this view.
        /// </summary>
        /// <remarks>
        /// This is used by the model to invoke the UI thread.
        /// </remarks>
        public static PathPlanningAndTrackingView Instance
        {
            get { return PathPlanningAndTrackingView.instance; }
            set { PathPlanningAndTrackingView.instance = value; }
        }

        /// <summary>
        /// MouseDown event handler.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Mouse button event.</param>
        private void UserControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // offsets within visualization frame
            const int OffsetY = 33;
            const int OffsetX = 33;

            var model = ((PathPlanningAndTrackingModel)this.PathPlanImage.DataContext);

            // mouse coordinates to map coordinates
            var p = e.GetPosition(this.PathPlanImage);
            var image = model.ImageSource;
            var xscale = image.Width / this.PathPlanImage.ActualWidth;
            var yscale = image.Height / this.PathPlanImage.ActualHeight;

            PathPlanningAgent.Target = new Pose2D(
                (int)((this.PathPlanImage.ActualWidth - p.Y + OffsetY) * yscale),
                (int)((this.PathPlanImage.ActualHeight - p.X + OffsetX) * xscale));
        }
    }
}
